--
-- Author: ping.wang
-- Date: 2019-07-19 11:00:25
--
-- PartnerQualityLvUpView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PartnerQualityLvUpScene

function ClassRef:init()

    self:refresh()

    self._ui.Button_up:addClickEventListener(function()
        if self._data:isMaxQuality() then
            display.pushToast(L("lua_code_text_349"))
        else
            if self._data:checkQualityCostItemsEnough() then
                executeFunc(self._onOperate, "startLvUpPartnerQuality", self._data:getId())
            else
                display.pushToast(L("tips_1990083"))
            end
        end
    end)

    self:registerViewEvent({
        {name = ViewEvent.Partner_DataChange, callback = handler(self, self._onPartnerDataChange)},
    })


    self._ui.Button_cancel:addClickEventListener(function()
        self:closeView()
    end)

end

function ClassRef:_onPartnerDataChange()
    self:refresh()
end

function ClassRef:refresh()
    self._playerAddAttrPanel = self._ui.Panel_content:findChild("Panel_1")
    self._max1 = self._ui.Panel_content:findChild("max_1")
    self._partnerAddAttrPanel = self._ui.Panel_content:findChild("Panel_2")
    self._max2 = self._ui.Panel_content:findChild("max_2") --达到最高级就显示这个 而不显示panel
    self._progressBar = self._ui.Panel_content:findChild("Panel_bar/LoadingBar")

    if self._data:isMaxQuality() then
        self._max1:setVisible(true)
        self._max2:setVisible(true)
        self._ui.Panel_content:findChild("Panel_bar/num"):setString("Max")
        self._progressBar:setPercent(100)
    else
        self._max1:setVisible(false)
        self._max2:setVisible(false)
        local haveNum, needNum = self._data:getQualityCostNum()

        if needNum > 0 and haveNum < needNum then
            self._progressBar:setPercent(haveNum/needNum * 100)
        else
            self._progressBar:setPercent(100)
        end

        if needNum > 0 then
            self._ui.Panel_content:findChild("Panel_bar/num"):setString(string.format("%s/%s",haveNum,needNum))
        else
            self._ui.Panel_content:findChild("Panel_bar/num"):setString("Max")
        end

        self._playerAddAttrPanel:setVisible(true)
        self._partnerAddAttrPanel:setVisible(true)


        local playerAddVals = self._data:getPlayerQualityPropShow()[self._data:getQuality()]

        for k, v in ipairs(playerAddVals) do
            local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
            self._playerAddAttrPanel:findChild("data_"..k):setString(name)
            self._playerAddAttrPanel:findChild("num_"..k):setString(value)
        end

        local partnerAddVals = self._data:getPartnerPropShow()[self._data:getQuality()]

        for k, v in ipairs(partnerAddVals) do
            if k <= 2 then
                local name, value = GD:getPropShowDataAndHandlerColon(v[1], v[2])
                self._partnerAddAttrPanel:findChild("data_"..k):setString(name)
                self._partnerAddAttrPanel:findChild("num_"..k):setString(value)
            end
        end
    end
end


return ClassRef
